from lib.utils import get_logger
from lib.convert import client
import os
from lib.config import config
import requests

logger = get_logger(__name__)


model = "doubao-seedream-3-0-t2i-250415"


def generate_image_by_text(title, content):
    logger.info(f"开始生成小红书图片")
    imagesResponse = client.images.generate(
        model=model,
        prompt=f"根据小红书标题：{title}，小红书正文：{content}，生成三张小红书图片，图片风格为小红书风格",
    )

    logger.info(f"小红书图片生成完成")

    if not os.path.exists(config.image_dir):
        os.makedirs(config.image_dir)

    for imgData in imagesResponse.data:
        url = imgData.url
        image_path = os.path.join(config.image_dir, f"{title}.png")
        response = requests.get(url)
        with open(image_path, "wb") as f:
            f.write(response.content)

    logger.info(f"小红书图片保存完成,图片路径是{config.image_dir}")
    return image_path
